package com.tnovoselec.android.feed.handler;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.os.SystemClock;
import android.util.Log;

import com.tnovoselec.android.feed.handler.entity.ChannelDto;

public class ChannelParser extends DefaultHandler {

    private static final String TAG_CHANNEL = "channel";
    private static final String TAG_ID = "id";
    private static final String TAG_TITLE = "title";
    private static final String TAG_IMAGE_URL = "image";
    private static final String TAG_URL = "url";
    private static final String TAG_DESCRIPTION = "description";
    private static final String TAG_PARENT_ID = "parent_id";

    private boolean inChannel = false;
    private boolean inId = false;
    private boolean inTitle = false;
    private boolean inImageUrl = false;
    private boolean inUrl = false;
    private boolean inDescription = false;
    private boolean inParentId = false;

    private List<ChannelDto> channels = new ArrayList<ChannelDto>();
    private ChannelDto item;

    private long parsingStartTime;

    private StringBuilder buffer;

    public ChannelParser() {
	super();
    }

    @Override
    public void startDocument() throws SAXException {
	parsingStartTime = SystemClock.elapsedRealtime();
    }

    @Override
    public void endDocument() throws SAXException {
	Log.i(getClass().getName(), "XML parsing took " + (SystemClock.elapsedRealtime() - parsingStartTime) / 1000f + " sec");
	Log.i(getClass().getName(), "Have " + channels.size() + " channels");
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
	buffer = new StringBuilder("");

	if (localName.equalsIgnoreCase(TAG_CHANNEL)) {
	    inChannel = true;
	    item = new ChannelDto();
	} else if (localName.equalsIgnoreCase(TAG_ID)) {
	    inId = true;
	} else if (localName.equalsIgnoreCase(TAG_TITLE)) {
	    inTitle = true;
	} else if (localName.equalsIgnoreCase(TAG_IMAGE_URL)) {
	    inImageUrl = true;
	} else if (localName.equalsIgnoreCase(TAG_URL)) {
	    inUrl = true;
	} else if (localName.equalsIgnoreCase(TAG_DESCRIPTION)) {
	    inDescription = true;
	} else if (localName.equalsIgnoreCase(TAG_PARENT_ID)) {
	    inParentId = true;
	}
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {

	if (inChannel) {
	    if (inTitle) {
		item.setTitle(buffer.toString());
	    } else if (inId) {
		item.setChannelId(buffer.toString());
	    } else if (inImageUrl) {
		item.setImageUrl(buffer.toString());
	    } else if (inUrl) {
		item.setUrl(buffer.toString());
	    } else if (inDescription) {
		item.setDescription(buffer.toString());
	    } else if (inParentId) {
		item.setParentId(Long.parseLong(buffer.toString()));
	    }
	}

	if (localName.equalsIgnoreCase(TAG_CHANNEL)) {
	    inChannel = false;
	    // item.setId(channels.size());
	    channels.add(item);
	} else if (localName.equalsIgnoreCase(TAG_TITLE)) {
	    inTitle = false;
	} else if (localName.equalsIgnoreCase(TAG_ID)) {
	    inId = false;
	} else if (localName.equalsIgnoreCase(TAG_IMAGE_URL)) {
	    inImageUrl = false;
	} else if (localName.equalsIgnoreCase(TAG_URL)) {
	    inUrl = false;
	} else if (localName.equalsIgnoreCase(TAG_DESCRIPTION)) {
	    inDescription = false;
	} else if (localName.equalsIgnoreCase(TAG_PARENT_ID)) {
	    inParentId = false;
	}
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
	buffer.append(String.valueOf(ch, start, length));
    }

    /**
     * Method used to fetch channels
     * 
     * @return List containing channels
     */
    public List<ChannelDto> getChannels() {
	return channels;
    }

}